<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>晴天医诊</title>
    <meta name="description" content="晴天医诊">
    <meta name="keywords" content="晴天医诊">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }

        .custom-tree-node {
            flex: 1;
            display: flex;
            align-items: center;
            justify-content: space-between;
            font-size: 14px;
            padding-right: 8px;
        }

        .el-tree-node__content {
            width: 50%;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">

    <div class="content-header">
        <h1>系统管理<small>菜单管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <el-button type="primary" class="butT" @click="addMenu()">添加一级菜单</el-button>
            <!-- <el-tree
                 :data="menuData"
                 node-key="id">
                       <span class="custom-tree-node" slot-scope="{ node, data }">
                         <span>{{ node.label }}</span>
                           <el-button type="text" size="mini" @click="addChildren(node,data)">添加子菜单</el-button>
                           <el-button type="text" size="mini" @click="delete(node,data)">删除菜单</el-button>


                       </span>
              </el-tree>-->

            <el-tree
                    :data="menuData"
                    node-key="id"
                    default-expand-all
                    :expand-on-click-node="true">
                  <span class="custom-tree-node" slot-scope="{ node, data }">
                    <span>{{ node.label }}</span>
                    <span>
                      <el-button type="text" size="mini" @click="() => addChildren(node,data)">新增</el-button>
                      <el-button type="text" size="mini" @click="() => remove(node, data)">删除</el-button>
                    </span>
                  </span>
            </el-tree>


            <div id="addMenuDiv">
                <el-dialog title="新增菜单" :visible.sync="dialogAddMenuVisible">
                    <el-form :model="menu">
                        <el-form-item label="菜单名称" :label-width="formLabelWidth">
                            <el-input v-model="menu.name"></el-input>
                        </el-form-item>
                        <el-form-item label="跳转地址" :label-width="formLabelWidth">
                            <el-input v-model="menu.linkUrl"></el-input>
                        </el-form-item>
                        <el-form-item label="图标" :label-width="formLabelWidth">
                            <el-input v-model="menu.icon"></el-input>
                        </el-form-item>

                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogAddMenuVisible = false">取 消</el-button>
                        <el-button type="primary" @click="handlerAddMenu()">确 定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            menuData: [],
            dialogAddMenuVisible: false,
            formLabelWidth: '120px',
            menu: {
                name: 'as',
                linkUrl: 'as',
                parentMenuId: 1
            }
        },
        created() {
            this.loadMenu();

        },
        methods: {
            remove(node, data) {
                const parent = node.parent;
                const children = parent.data.children || parent.data;
                const index = children.findIndex(d => d.id === data.id);
                this.$confirm('是否删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete("/menu/delete.do?id=" + data.id).then((res) => {
                        this.$message({
                            message: res.data.message,
                            type: res.data.flag ? "success" : "error"
                        });
                        if(res.data.flag) {
                            children.splice(index, 1);
                        }
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            renderContent(h, {node, data, store}) {
                return (
                    '<span class="custom-tree-node"><span>{node.label}</span><span>' +
                    '<el-button size="mini" type="text" on-click={ () => this.addChildren(node, data) }>新增</el-button>' +
                    '<el-button size="mini" type="text" on-click={ () => this.remove(node, data) }>删除</el-button>' +
                    '</span></span>');
            },
            //添加子菜单
            addChildren(node, data) {
                this.menu = {}
                this.dialogAddMenuVisible = true;
                //将当前菜单id作为要添加菜单的父亲
                this.menu.parentMenuId = data.id;
            },
            //弹出添加菜单框
            addMenu() {
                //重置表单
                this.menu = {}
                this.dialogAddMenuVisible = true;
            },
            //菜单数据保存到数据库
            handlerAddMenu() {
                axios.post("/menu/add.do", this.menu).then((res) => {
                    this.dialogAddMenuVisible = false;
                    this.$message({
                        message: res.data.message,
                        type: res.data.flag ? "success" : "error"
                    });
                    //刷新页面
                    this.loadMenu();
                    this.menu.parentMenuId = null;
                });

            },
            loadMenu() {
                axios.get("/menu/getMenus.do?id=1").then((res) => {
                    this.menuData = res.data.data;
                    alert(res.data.data)
                })
            },
            delete(node, data) {


            }
        }
    })
</script>
</html>
